package com.education.welco.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtils;
import com.education.welco.pojo.SAnswering;
import com.education.welco.pojo.SQuestionsStu;
import com.education.welco.service.ISAnsweringService;
import com.education.welco.service.ISQuestionsStuService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 学生提问 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@Api(tags = {"PC-学生提问"})
@RequestMapping("/sQuestionsStu")
public class SQuestionsStuController extends BaseController {

    @Autowired
    private ISQuestionsStuService questionsStuService;
    @Autowired
    private ISAnsweringService answeringService;


    /**
     *  根据当前登录用户id 查询消息列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(SQuestionsStu questionsStu, Integer pageSize, Integer pageNum) {
        if(pageSize != null && pageNum != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        //根据当前登录老师id  和  提问类型查询出列表
        if(questionsStu.getTeacherId() != null && questionsStu.getIsDisplay() != null && !"".equals(questionsStu.getIsDisplay())){
            //学生提问信息  以及 学生的信息 班 年级 姓名
            List<SQuestionsStu> list = questionsStuService.selectSQuestionsStuListOther(questionsStu);
            for (SQuestionsStu sQuestionsStu : list) {
                //改变创建日期字段格式
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = null;
                if (sQuestionsStu.getCreateTime() != null) {
                    format = sdf.format(sQuestionsStu.getCreateTime());
                }
//                sQuestionsStu.setCreateTime(sQuestionsStu.getCreateTime().split("\\.")[0]);
                sQuestionsStu.setSimpleCreateTime(format);
            }
            return new ApiReturnObject("200", "查询成功", list);
        }else {
            return new ApiReturnObject("201","缺少相关参数","");
        }
    }

    /**
     *  根据当前登录用户id 查询消息列表
     */
    @GetMapping("/listPage")
    public ApiReturnObject list2(SQuestionsStu questionsStu, Integer pageSize, Integer pageNum) {
        //根据当前登录老师id  和  提问类型查询出列表
        if(questionsStu.getTeacherId() != null && questionsStu.getIsDisplay() != null && !"".equals(questionsStu.getIsDisplay())){
            //学生提问信息  以及 学生的信息 班 年级 姓名
            if(pageSize != null && pageNum != null){
                PageHelper.startPage(pageNum,pageSize);
            }
            List<SQuestionsStu> list = questionsStuService.selectSQuestionsStuListOther(questionsStu);
            for (SQuestionsStu sQuestionsStu : list) {
                //改变创建日期字段格式
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = null;
                if (sQuestionsStu.getCreateTime() != null) {
                    format = sdf.format(sQuestionsStu.getCreateTime());
                }
//                sQuestionsStu.setCreateTime(sQuestionsStu.getCreateTime().split("\\.")[0]);
                sQuestionsStu.setSimpleCreateTime(format);
            }
            PageInfo<SQuestionsStu> pageInfo = new PageInfo<>(list);
            return new ApiReturnObject("200", "查询成功", pageInfo);
        }else {
            return new ApiReturnObject("201","缺少相关参数","");
        }
    }


    /**
     *  根据当前登录用户id 查询消息列表
     */
    @GetMapping("/listOther")
    public ApiReturnObject listOther(SQuestionsStu questionsStu) {
        //根据当前登录老师id  和  提问类型查询出列表
        if(questionsStu.getTeacherId() != null && questionsStu.getIsDisplay() != null && !"".equals(questionsStu.getIsDisplay())){
            //学生提问信息  以及 学生的信息 班 年级 姓名
            List<SQuestionsStu> list = questionsStuService.selectSQuestionsStuListOther(questionsStu);
            for (SQuestionsStu sQuestionsStu : list) {
                //改变创建日期字段格式
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = null;
                if (sQuestionsStu.getCreateTime() != null) {
                    format = sdf.format(sQuestionsStu.getCreateTime());
                }
                sQuestionsStu.setSimpleCreateTime(format);
            }
//            ArrayList<SQuestionsStu> sQuestionsStus = new ArrayList<>();
            HashMap<String, Object> map = new HashMap<>();
            /*if(list.size() > 1){
                sQuestionsStus.add(list.get(0));
                list.remove(0);
            }*/
            map.put("list",list);
//            map.put("sQuestionsStus",sQuestionsStus);
            return new ApiReturnObject("200", "查询成功", map);
        }else {
            return new ApiReturnObject("201","缺少相关参数","");
        }
    }


    /**
     *  学生提问详情    对提问进行回复
     */
    @PostMapping("/selectOneUpdate")
    public ApiReturnObject selectOneUpdate(@RequestBody SQuestionsStu questionsStu) {
        //判断回复 还是 页面初始化查询
        if(questionsStu.getTeacherId() != null){
            if(questionsStu.getId() != null  && questionsStu.getTeacherContent() != null && !"".equals(questionsStu.getTeacherContent()) ){
                //点击回复进行修改  对回复状态进行修改
                questionsStu.setIsDisplay("1");
                questionsStuService.updateSQuestionsStu(questionsStu);
                //查询出下一条提问  根据userId  并且  没有进行回复的  最新的一条
                return query(questionsStu);
            }
            //页面初始化 进行查询
            return query(questionsStu);
        }
        return new ApiReturnObject("201","缺少teacherId","");
    }


    /**
     *  公共方法 进行查询该教师有没有未回复的信息
     */
    private ApiReturnObject query(SQuestionsStu questionsStu){
        List<SQuestionsStu> sQuestionsStus = questionsStuService.selectSQuestionsStuByuserId(questionsStu.getTeacherId());
        //格式化时间
        for (SQuestionsStu questionsStus : sQuestionsStus) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(questionsStus.getCreateTime());
            questionsStus.setSimpleCreateTime(format);
        }
        //回复并进行查询
        if(questionsStu.getId() != null  && questionsStu.getTeacherContent() != null && !"".equals(questionsStu.getTeacherContent())){
            if(sQuestionsStus != null && sQuestionsStus.size() > 0){
                return new ApiReturnObject("200","回复成功",sQuestionsStus.get(0));
            }else {
                return new ApiReturnObject("202","回复成功","暂无提问");
            }
        }
        //只进行查询
        if(sQuestionsStus != null && sQuestionsStus.size() > 0){
            return new ApiReturnObject("200","查询成功",sQuestionsStus.get(0));
        }else {
            return new ApiReturnObject("202","查询成功","暂无提问");
        }
    }




    /**
     *  新增保存消息
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SQuestionsStu questionsStu) {
        questionsStu.setCreateTime(new Date());
        questionsStu.setSynch("0");
        questionsStuService.insertSQuestionsStu(questionsStu);
        return new ApiReturnObject("200","添加成功","");
    }


    /**
     *  修改消息  进行回复
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SQuestionsStu questionsStu) {
        if(questionsStu.getId() != null ){
            //先修改 其状态
            questionsStu.setIsDisplay("1");
            questionsStuService.updateSQuestionsStu(questionsStu);
            return new ApiReturnObject("200","回复成功","");
        }
        return new ApiReturnObject("201","缺少id","");
    }

    /**
     *  回复并添加到课程问答
     */
    @PostMapping("/updateAll")
    public ApiReturnObject editSaveTwo(@RequestBody SQuestionsStu questionsStu) {
        if(questionsStu.getId() != null ){
            if(questionsStu.getTeacherContent() != null && !"".equals(questionsStu.getTeacherContent())){
                //先修改 其状态
                questionsStu.setIsDisplay("1");
                questionsStuService.updateSQuestionsStu(questionsStu);
            }
            //查询同步状态
            SQuestionsStu sQuestionsStu = questionsStuService.selectSQuestionsStuById(questionsStu.getId());
            if("1".equals(sQuestionsStu.getSynch())){
                //已同步
                return new ApiReturnObject("202","该提问已同步","");
            }else {
                //未同步 修改状态
                SQuestionsStu sQuestionsStu1 = new SQuestionsStu();
                sQuestionsStu1.setId(questionsStu.getId());
                sQuestionsStu1.setSynch("1");
                questionsStuService.updateSQuestionsStu(sQuestionsStu1);

                //新建课程问答对象  problemTitle    problemContent  userId 教师id    创建时间  schoolId
                SAnswering answering = new SAnswering();
                answering.setProblemTitle(sQuestionsStu.getQuestionContent());
                answering.setProblemContent(sQuestionsStu.getTeacherContent());
                answering.setCreateTime(new Date());
                answering.setUserId(sQuestionsStu.getTeacherId());
                answering.setCatalogId(sQuestionsStu.getCatalogId());
                answering.setIsdisplay("1");
                answering.setIsStu("1");
                answering.setSynch("1");
                answeringService.insertSAnswering(answering);
                return new ApiReturnObject("200","同步成功","");
            }
        }
        return new ApiReturnObject("201","缺少id","");
    }


    /**
    *   添加到课程问答
    */
    @GetMapping("/updateOther")
    public ApiReturnObject updateOther(Integer id ){
        if(id != null ){
            SQuestionsStu sQuestionsStu = questionsStuService.selectSQuestionsStuById(id);
            //新建课程问答对象  problemTitle    problemContent  userId 教师id    创建时间  schoolId
            SAnswering answering = new SAnswering();
            answering.setProblemTitle(sQuestionsStu.getQuestionContent());
            answering.setProblemContent(sQuestionsStu.getTeacherContent());
            answering.setCreateTime(new Date());
            answering.setUserId(sQuestionsStu.getTeacherId());
            answering.setCatalogId(sQuestionsStu.getCatalogId());
            answering.setIsdisplay("1");
            answering.setIsStu("1");
            answering.setSynch("1");
            answeringService.insertSAnswering(answering);
        }
        return new ApiReturnObject("201","缺少参数id","");
    }



    /*删除消息*/
//    @PostMapping("/remove")
//    public ApiReturnObject remove(String ids) {
//        return toAjax(sysInfoMsgService.deleteSysInfoMsgByIds(ids));
//    }

}